<template>
  <el-row class="x-container">
    <el-col :md="18">
      <div class="card-wrap">
        <span class="resultTip" v-if="!isEmpty(keyword)">> 共找到{{total}}篇与"{{keyword}}"相关的文章</span>
        <el-card shadow="never">
          <div slot="header" style="text-align: right" class="card-header">
            <span class="refresh" @click="refresh" style="float: left">
              <i class="el-icon-refresh"></i>
            </span>
            <el-radio-group
              v-model="order"
              @change="handleOrderChange"
              size="mini"
              fill="#5fb878"
            >
              <el-radio-button :label="0">最新</el-radio-button>
              <el-radio-button :label="1">最热</el-radio-button>
            </el-radio-group>
          </div>
          <ul class="post-list">
            <li
              v-for="(article, index) in articles"
              :key="index"
              class="post-item"
            >
              <span v-if="article.badge" class="layui-badge">推荐</span>
              <a :href="'/articles/' + article.author + '/' + article.id">
                <h3 class="title">{{ article.title }}</h3>
                <p class="abstract">{{ article.summary }}</p>
              </a>
              <div class="details">
                <a :href="'/articles/' + article.author" class="left">
                  <span style="color: #5fb878; text-decoration: underline">{{
                    article.author
                  }}</span>
                </a>
                <div class="right">
                  <span>{{ format(article.createTime) }}</span>
                  <span>
                    <i
                      class="layui-icon layui-icon-praise"
                      style="vertical-align: bottom"
                    ></i>
                    {{ article.hits }}
                  </span>
                  <span>
                    <i class="el-icon-chat-dot-round"></i>
                    {{ article.comments }}
                  </span>
                  <span>
                    <i class="el-icon-view"></i>
                    {{ article.views }}
                  </span>
                </div>
              </div>
            </li>
          </ul>
          <el-pagination
            class="page-box"
            background
            layout="prev, pager, next"
            :hide-on-single-page="true"
            :total="total"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </el-card>
      </div>
    </el-col>
    <el-col :md="6">
      <div class="card-wrap">
        <aside-right></aside-right>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import AsideRight from "./AsideRight.vue";
import request from "@/utils/request";
import { parseTime } from "@/utils";
import validator from "@/utils/validator";

export default {
  name: "Body",
  props: {
    kw: String
  },
  components: {
    AsideRight,
  },
  data() {
    return {
      articles: [],
      current: 1,
      order: 0,
      total: 10,
      size: 8,
      keyword: null
    };
  },
  methods: {
    // 处理当前页切换
    handleCurrentChange(cur) {
      this.requestArticleList({
        current: cur,
        size: this.size,
        order: this.order,
      });
    },
    handleOrderChange(order) {
      this.requestArticleList({
        current: 1,
        order: order
      });
    },
    requestArticleList(param) {
      console.log("param => ",param)
      param = param || { current: 1, size: this.size, order: this.order };
      request.get("/articles", { params: param }).then((res) => {
        this.articles = res.data.list;
        this.total = res.data.total;
      });
    },
    format(time) {
      return parseTime(time);
    },
    refresh() {
      this.keyword = null;
      this.$router.push("/home");
      this.requestArticleList();
    },
    isEmpty(val) {
      return validator.isEmpty(val);
    }
  },
  created() {

  },
  watch: {
    kw(val) {
      this.keyword = val;
    },
    immediate: true,
  },
};
</script>

<style scoped>
  .el-main {
    padding: 0;
  }

  .x-container {
    padding: 0 2%;
  }

  a {
    cursor: pointer;
  }

  .resultTip {
    color: #999;
    padding: 2px;
    font-size: 13px;
    font-family: 'Microsoft Yahei';
  }

  .article-list > li {
    position: relative;
    height: auto;
    line-height: 22px;
    padding: 15px;
    background-color: #ffffff;
    border-radius: 2px;
    list-style: none;
  }
  .article-list > li + li {
    border-top: 1px solid #f0f0f0;
  }

  .article-list .layui-badge {
    height: 16px;
    line-height: 16px;
    padding: 0 5px;
    margin-right: 10px;
    font-size: 12px;
    border: 1px solid #5fb878;
    background: none;
    color: #5fb878;
  }

  .article-list .title {
    display: inline-block;
    font-weight: 500;
    font-size: 18px;
    line-height: 28px;
    color: #212121;
    position: relative;
    vertical-align: middle;
  }

  .article-list .title:hover {
    color: #5fb878;
  }

  .article-list .summary {
    overflow: hidden;
    text-overflow: ellipsis;
    /* 多行溢出 */
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    color: #888888;
    margin: 10px;
    line-height: 1.2;
    font-size: 13px;
    font-family: "Microsoft YaHei", "PingFang SC Semibold", "IBM Plex Mono",
      monospace;
    text-indent: 1em;
  }

  .article-list .info {
    height: 32px;
    line-height: 32px;
  }

  .article-list .info img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
  }

  .article-list .info span {
    padding: 5px;
    font-size: 12px;
    color: #999;
  }

  .article-list .info span.active {
    color: #5fb878;
  }

  .article-list .info .sl {
    float: right;
  }

  .article-list .info .sl span {
    cursor: pointer;
  }
</style>